package com.tsd.anon;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.constants.Constants;
import com.tsd.core.constants.JwtConstants;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.jwt.AccessToken;
import com.tsd.core.jwt.JwtUtils;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.HttpUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.jwt.JwtHelper;
import com.tsd.system.entity.SysAccountSetExt;
import com.tsd.system.entity.SysRegUserExt;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysAccountSetService;
import com.tsd.system.service.SysRegUserService;
import com.tsd.system.service.VerifyCodeGenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 描述：匿名读取数据的控制器
 * 未登录时需要获取数据的方法都写这里
 *
 * @author david
 * @date 2021/09/08 21:25:26
 */
@Controller
@Scope("prototype")
@RequestMapping("/anon")
@Api(tags = "匿名注册", description = "匿名注册的控制器")
public class AnonRegController extends BaseController {

    @Resource
    private VerifyCodeGenService verifyCodeGenService;
    @Resource
    private SysAccountSetService sysAccountSetService;
    @Resource
    private SysRegUserService sysRegUserService;

    @ApiOperation(value = "注册客户")
    @ApiImplicitParams({})
    @ResponseBody
    @RequestMapping(value = "/regClient.action", method = RequestMethod.POST)
    public HttpResult<Object> regClient() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String verifyCode = HlpUtils.getString(map, "verifyCode");
            super.checkEmpty(verifyCode, "缺少参数：verifyCode");
            String verifySessionId = HlpUtils.getString(map, "verifySessionId");
            boolean verify = verifyCodeGenService.verify(verifyCode, verifySessionId);
            if (!verify) {
                throw new HlpException("验证码错误！");
            }
//            SysClientExt client = JSON.parseObject(JSON.toJSONString(map), SysClientExt.class);
//            super.checkEmpty(client.getName(), "请输入公司/组织名称");
//            super.checkEmpty(client.getCode(), "请输入账套标识");
//            super.checkEmpty(client.getLinker_name(), "请输入联系人");
//            super.checkEmpty(client.getLinker_mobile(), "请输入联系电话");
            DataSourceContextHolder.setDataSourceType(null);
//            sysClientService.saveSysClient4RegAccount(client, null);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取账套状态")
    @ApiImplicitParams({})
    @ResponseBody
    @RequestMapping(value = "/getAccountSetState.action", method = RequestMethod.POST)
    public HttpResult<Object> getAccountSetState() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String account_set_sid = HttpUtil.getAccountSet(request);
            super.checkEmpty(account_set_sid, "请输入账套标识");
            DataSourceContextHolder.setDataSourceType(null);
            SysAccountSetExt t = sysAccountSetService.queryBySid(account_set_sid);
            if (t != null) {
                hr.setData(t.getState());
                hr.setCode(ErrorCode.CODE_SUCCESS);
            } else {
                hr.setMessage("账套不存在");
                hr.setCode(ErrorCode.CODE_NO_DATA);
            }
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "注册用户")
    @ResponseBody
    @RequestMapping(value = "/regUser.action", method = RequestMethod.POST)
    public HttpResult<Object> regUser() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
//            String verifyCode = HlpUtils.getString(map, "verifyCode");
//            super.checkEmpty(verifyCode, "缺少参数：verifyCode");
//            String verifySessionId = HlpUtils.getString(map, "verifySessionId");
//            boolean verify = verifyCodeGenService.verify(verifyCode, verifySessionId);
//            if (!verify) {
//                throw new HlpException("验证码错误！");
//            }
            String accountSetSid =  HttpUtil.getAccountSet(request);
            SysRegUserExt userExt = JSON.parseObject(JSON.toJSONString(map), SysRegUserExt.class);
            super.checkEmpty(userExt.getName(), "请输入用户名");
            super.checkEmpty(userExt.getLogin_id(), "请输入登录账号");
            super.checkEmpty(userExt.getPassword(), "请输入密码");
            DataSourceContextHolder.setDataSourceType(accountSetSid);
            sysRegUserService.saveSysRegUser(userExt, null);
            if (map.containsKey("auto_login")) {
                userExt.setPassword(null);
                AccessToken accessToken = new AccessToken();
                SysUserExt opUser = new SysUserExt(userExt);
                String token = JwtHelper.createJWT(JwtConstants.TYPE_REG_USER, opUser);
                accessToken.setToken(token);
                accessToken.setExpires_in(JwtUtils.getTokenExpiredSeconds());
                userExt.setAccessToken(accessToken);
                hr.setData(userExt);
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "注册用户登录")
    @ResponseBody
    @RequestMapping(value = "/regUserLogin.action", method = RequestMethod.POST)
    public HttpResult<Object> regUserLogin() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
//            String verifyCode = HlpUtils.getString(map, "verifyCode");
//            super.checkEmpty(verifyCode, "缺少参数：verifyCode");
//            String verifySessionId = HlpUtils.getString(map, "verifySessionId");
//            boolean verify = verifyCodeGenService.verify(verifyCode, verifySessionId);
//            if (!verify) {
//                throw new HlpException("验证码错误！");
//            }
            String accountSetSid =  HttpUtil.getAccountSet(request);
            DataSourceContextHolder.setDataSourceType(accountSetSid);
            SysRegUserExt userExt = sysRegUserService.regUserLogin(map);
            AccessToken accessToken = new AccessToken();
            SysUserExt opUser = new SysUserExt(userExt);
            String token = JwtHelper.createJWT(JwtConstants.TYPE_REG_USER, opUser);
            accessToken.setToken(token);
            accessToken.setExpires_in(JwtUtils.getTokenExpiredSeconds());
            userExt.setAccessToken(accessToken);
            hr.setData(userExt);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
